package com.iyue.servlet.taglibs;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.iyue.Config;
import com.iyue.SpringHelper;

/**
 * 《截图》实现类
 * 
 * @author 孙晓晔
 * @version $Id: ImageTagUtil.java,v 1.1 2009/02/16 07:53:06 sunxy Exp $
 */
public abstract class ImageTagUtil extends TagSupport {

	private static final long serialVersionUID = 8635509023843676289L;

	/**
	 * 源图地址
	 */
	private String src;
	/**
	 * 期望宽度
	 */
	private String width;
	/**
	 * 期望高度
	 */
	private String height;
	/**
	 * 样式
	 */
	private String styleClass;
	/**
	 * 提示
	 */
	private String alt;
	/**
	 * 缺省图片
	 */
	private String file;

	/**
	 * 计算图片标签
	 * 
	 * @param resize
	 * @return String
	 */
	protected String img(String resize) {
		StringBuffer sb = new StringBuffer();
		sb.append("<img src=\"");
		sb.append(src(resize));
		sb.append("\"");

		if (getStyleClass() != null) {
			sb.append(" class=\"");
			sb.append(getStyleClass());
			sb.append("\"");
		}

		if (getAlt() != null) {
			sb.append(" alt=\"");
			sb.append(getAlt());
			sb.append("\"");
		}

		sb.append(" />");

		return sb.toString();
	}

	/**
	 * 计算图片地址
	 * 
	 * @param resize
	 * @return StringBuffer
	 */
	public StringBuffer src(String resize) {
		Config cfg = (Config) SpringHelper.getBean("test");

		StringBuffer sb = new StringBuffer();
		sb.append(cfg.getImg());

		if (getFile() == null || getFile().equals("")) {
			setFile("/images/default.jpg");
		}

		String url = getSrc();
		if (url == null || url.equals("") || url.lastIndexOf("/") < 0) {
			sb.append(getFile());
			return sb;
		}

		sb.append("/img/");
		sb.append(getWidth() + resize + getHeight());

		String filename = url.substring(url.lastIndexOf("/") + 1);
		String aa;
		for (int i = 0, len = filename.length() - 4; i < len; i += 4) {
			aa = filename.substring(i, i + 1).toLowerCase();
			if (aa.matches("([\\d|a-z]+)")) {
				sb.append("/");
				sb.append(aa);
			}
		}
		sb.append("/");

		sb.append(filename);

		return sb;
	}

	public int doEndTag() throws JspException {
		return (EVAL_PAGE);
	}

	public String getHeight() {
		if (height == null) {
			height = "100";
		}

		return height;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	public String getWidth() {
		if (width == null) {
			width = "100";
		}

		return width;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public String getAlt() {
		return alt;
	}

	public void setAlt(String alt) {
		this.alt = alt;
	}

	public String getSrc() {
		return src;
	}

	public void setSrc(String src) {
		this.src = src;
	}

	public String getStyleClass() {
		return styleClass;
	}

	public void setStyleClass(String styleClass) {
		this.styleClass = styleClass;
	}

	public String getFile() {
		return file;
	}

	public void setFile(String file) {
		this.file = file;
	}

}
